package com.objectdraw.server;

import java.net.Socket;
import java.util.HashMap;

import com.objectdraw.server.commands.ColorChangeCommand;
import com.objectdraw.server.commands.Command;
import com.objectdraw.server.commands.ConnectCommand;
import com.objectdraw.server.commands.ControlCommand;
import com.objectdraw.server.commands.DisconnectCommand;
import com.objectdraw.server.commands.FullUpdateCommand;
import com.objectdraw.server.commands.KeyboardCommand;
import com.objectdraw.server.commands.MouseCommand;
import com.objectdraw.server.commands.SaveRequestCommand;
import com.objectdraw.server.commands.ToolChangeCommand;

public class ObjectDrawServer {

	private static ObjectDrawServer instance = null;
	private HashMap<String,Socket> clients = new HashMap<String,Socket>();
	
	private ObjectDrawServer() {

	}

	// TODO: reconsider synchronization... there might be a better way
	/**
	 * Use this method to get an instance of the ObjectDraw server
	 * 
	 * @return
	 */
	public synchronized static ObjectDrawServer getInstance() {
		if (instance == null) {
			instance = new ObjectDrawServer();
		}
		return instance;
	}
	
	public static Command getCommandHandlers() {
		// TODO: Is there a better way to load the Commands??
		ConnectCommand connect = new ConnectCommand();
		DisconnectCommand disconnect = new DisconnectCommand();
		ColorChangeCommand color = new ColorChangeCommand();
		ControlCommand control = new ControlCommand();
		FullUpdateCommand update = new FullUpdateCommand();
		KeyboardCommand keyboard = new KeyboardCommand();
		MouseCommand mouse = new MouseCommand();
		SaveRequestCommand save = new SaveRequestCommand();
		ToolChangeCommand tool = new ToolChangeCommand();

		connect.setSuccessor(disconnect);
		disconnect.setSuccessor(color);
		color.setSuccessor(control);
		control.setSuccessor(update);
		update.setSuccessor(keyboard);
		keyboard.setSuccessor(mouse);
		mouse.setSuccessor(save);
		save.setSuccessor(tool);
		return connect;
	}
	
	public void addClient(Socket client, String username) {
		clients.put(username, client);
		
	}

	public Socket removeClient(String uname) {
		return clients.remove(uname);
	}

}
